<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8" />
  <meta name="referrer" content="never" />
  
  
  <meta name="description" content="Middleware,Message Queue" />
  

  
  
  <meta name="google-site-verification" content="NCXVSqxqB-os803-VFMtIEd1SUNJVOIjctCfNYUwD0w" />
  <meta name="baidu-site-verification" content="Nzwx7HuplM" />
  
  <meta name="msvalidate.01" content="9E9EA92F7E5327E1FE8A5196D5253664" />
  
  
  
  <title>消息中间件 | masuo777的博客</title>
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
  <meta name="description" content="Middleware,Message Queue">
<meta property="og:type" content="article">
<meta property="og:title" content="消息中间件">
<meta property="og:url" content="https://masuo777.github.io/2022/01/18/MQ/index.html">
<meta property="og:site_name" content="masuo777的博客">
<meta property="og:description" content="Middleware,Message Queue">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://masuo-github-image.oss-cn-beijing.aliyuncs.com/image/20220322092143.jpeg">
<meta property="og:image" content="https://masuo-github-image.oss-cn-beijing.aliyuncs.com/image/20220321095802.png">
<meta property="og:image" content="https://masuo-github-image.oss-cn-beijing.aliyuncs.com/image/20220321100309.png">
<meta property="og:image" content="https://masuo-github-image.oss-cn-beijing.aliyuncs.com/image/20220322113118.png">
<meta property="article:published_time" content="2022-01-18T09:27:50.237Z">
<meta property="article:modified_time" content="2022-03-22T03:31:27.067Z">
<meta property="article:author" content="John Doe">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://masuo-github-image.oss-cn-beijing.aliyuncs.com/image/20220322092143.jpeg">
  
    <link rel="alternative" href="/atom.xml" title="masuo777的博客" type="application/atom+xml">
  
  
    <link rel="icon" href="http://7u2hdm.com1.z0.glb.clouddn.com/favicon.ico">
  
  
<link rel="stylesheet" href="/css/style.css">

  

  
  <!-- baidu webmaster push -->
  <script src='//push.zhanzhang.baidu.com/push.js'></script>
<meta name="generator" content="Hexo 5.4.0"></head>
<body class="home blog custom-background custom-font-enabled single-author">
  <div id="page" class="hfeed site">
      <header id="masthead" class="site-header" role="banner">
    <hgroup>
      <h1 class="site-title">
        <a href="/" title="masuo777的博客" rel="home">masuo777的博客</a>
      </h1>
      
        <script type="text/javascript" src="http://api.hitokoto.us/rand?encode=js&charset=utf-8"></script>
        <h2 class="site-description"><script>hitokoto();</script></h2>
      
    </hgroup>

    <nav id="site-navigation" class="main-navigation" role="navigation">
            <button class="menu-toggle">菜单</button>
            <a class="assistive-text" href="/#content" title="跳至内容">跳至内容</a><!--TODO-->
            <div class="menu-main-container">
                <ul id="menu-main" class="nav-menu">
                
                    <li class="menu-item menu-item-type-post_type menu-item-object-page"><a href="/">MDS</a></li>
                
                    <li class="menu-item menu-item-type-post_type menu-item-object-page"><a href="/todo">ToDo</a></li>
                
                    <li class="menu-item menu-item-type-post_type menu-item-object-page"><a href="/images">images</a></li>
                
                    <li class="menu-item menu-item-type-post_type menu-item-object-page"><a href="/about">ME</a></li>
                
                    <li class="menu-item menu-item-type-post_type menu-item-object-page"><a href="/works">作品集</a></li>
                
                    <li class="menu-item menu-item-type-post_type menu-item-object-page"><a href="/tools">工具集</a></li>
                
                    <li class="menu-item menu-item-type-post_type menu-item-object-page"><a target="_blank" rel="noopener" href="https://github.com/Masuo777">my github</a></li>
                
                </ul>
            </div>
    </nav>
</header>
      <div id="main" class="wrapper">
        <div id="primary" class="site-content"><div id="content" role="main"><article id="post-MQ" class="post-MQ post type-post status-publish format-standard hentry">
    <!---->

      <header class="entry-header">
        
        
  
    <h1 class="entry-title article-title">
      消息中间件
    </h1>
  

        
        <div class="comments-link">
            
            <a href="/2022/01/18/MQ/#comments" class="leave-reply">评论</a>
            
            <a href="javascript:void(0);" data-url="https://masuo777.github.io/2022/01/18/MQ/" data-id="cl3jit8nt000j2ojb9yeg843d" class="leave-reply bdsharebuttonbox" data-cmd="more">分享</a>
        </div><!-- .comments-link -->
      </header><!-- .entry-header -->

    <div class="entry-content">
      
        <h1 id="MQ"><a href="#MQ" class="headerlink" title="MQ"></a>MQ</h1><blockquote>
<p>消息队列已经逐渐称为企业IT系统内部通信的核心手段。具有<strong>低耦合</strong>，<strong>可靠投递</strong>，<strong>广播</strong>，<strong>流量控制</strong>，最终一致性等一系列功能，成为<strong>异步RPC</strong>的最主要手段之一。</p>
<p>消息中间件利用高效可靠的消息传递机制进行平台无关的数据交流，并基于数据通信来进行分布式系统的集成。通过提供消息传递和消息排队模型，可以在分布式环境下扩展进程间的通信。</p>
<p>在MQ中，最常见的角色就是<strong>生产者</strong>（producer）和<strong>消费者</strong>（consumer）以及服务者（broker/server）。</p>
<p>参考：</p>
<p><a target="_blank" rel="noopener" href="https://zhuanlan.zhihu.com/p/62936911%E3%80%81https://zhuanlan.zhihu.com/p/92815904%E3%80%81https://zhuanlan.zhihu.com/p/72728396%E3%80%81https://www.jianshu.com/p/8c231858c327">https://zhuanlan.zhihu.com/p/62936911、https://zhuanlan.zhihu.com/p/92815904、https://zhuanlan.zhihu.com/p/72728396、https://www.jianshu.com/p/8c231858c327</a></p>
</blockquote>
<span id="more"></span>

<h2 id="组件"><a href="#组件" class="headerlink" title="组件"></a>组件</h2><h3 id="Broker"><a href="#Broker" class="headerlink" title="Broker"></a>Broker</h3><blockquote>
<p>消息服务器，即server，给生产者和消费者提供消息服务。</p>
</blockquote>
<h3 id="Producer"><a href="#Producer" class="headerlink" title="Producer"></a>Producer</h3><blockquote>
<p>消息生产者，业务的调用方，生产消息，发送给Broker。</p>
</blockquote>
<h3 id="Consumer"><a href="#Consumer" class="headerlink" title="Consumer"></a>Consumer</h3><blockquote>
<p>消息消费者，业务的处理方，获取消息，进行业务上的逻辑处理。</p>
</blockquote>
<h3 id="Topic"><a href="#Topic" class="headerlink" title="Topic"></a>Topic</h3><blockquote>
<p>主题，发布订阅模式下的消息统一汇集地，不同生产者向topic发送消息，由MQ服务器发送给不同的订阅者,实现消息广播.</p>
</blockquote>
<h3 id="Queue"><a href="#Queue" class="headerlink" title="Queue"></a>Queue</h3><blockquote>
<p>队列，PTP模式下，特定生产者向特定queue发送消息，消费者订阅特定的queue完成指定消息的接收。</p>
</blockquote>
<h3 id="Message"><a href="#Message" class="headerlink" title="Message"></a>Message</h3><blockquote>
<p>消息，根据不同的通信协议的固定格式进行编码的数据包，封装业务数据，实现消息的传输。</p>
</blockquote>
<hr>
<p>现在国内最常用的消息中间件就是Rocket MQ和Kafka。</p>
<h1 id="Active-MQ"><a href="#Active-MQ" class="headerlink" title="Active MQ"></a>Active MQ</h1><blockquote>
<p>渐趋淘汰，已经处于被淘汰的边缘（在国内市场）。</p>
</blockquote>
<h1 id="Rabbit-MQ"><a href="#Rabbit-MQ" class="headerlink" title="Rabbit MQ"></a>Rabbit MQ</h1><blockquote>
<p>同active MQ，渐渐被市场淘汰。</p>
</blockquote>
<p><img src="https://masuo-github-image.oss-cn-beijing.aliyuncs.com/image/20220322092143.jpeg" alt="rabbitmq"></p>
<ul>
<li>Broker：消息队列服务进程，包括Exchange和Queue</li>
<li>Producer：消息生产者，生产方客户端，将消息发送到服务端</li>
<li>Consumer：消息消费者，消费方客户端，接收服务器发送的消息</li>
<li>Exchange：交换机，接收生产者发送的消息，并且可以将消息发送到指定的队列</li>
<li>Queue：队列，消息存储队列，可以将消息发送到消费者</li>
</ul>
<h2 id="安装"><a href="#安装" class="headerlink" title="安装"></a>安装</h2><h3 id="CentOS"><a href="#CentOS" class="headerlink" title="CentOS"></a>CentOS</h3><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#</span><span class="bash"> 安装依赖</span></span><br><span class="line">rpm -ivh erlang-20.3.8.6-1.el6.x86_64.rpm</span><br><span class="line"></span><br><span class="line">yum -y install epel-release</span><br><span class="line"></span><br><span class="line">yum -y install socat</span><br><span class="line"><span class="meta"></span></span><br><span class="line"><span class="meta">#</span><span class="bash"> 安装rabbit MQ</span></span><br><span class="line">rpm -ivh rabbitmq-server-3.7.7-1.el6.noarch.rpm</span><br><span class="line"><span class="meta"></span></span><br><span class="line"><span class="meta">#</span><span class="bash"> 启动</span></span><br><span class="line">rabbitmq-server -detached</span><br><span class="line"><span class="meta"></span></span><br><span class="line"><span class="meta">#</span><span class="bash"> 停止</span></span><br><span class="line">rabbitmqctl stop</span><br><span class="line"><span class="meta"></span></span><br><span class="line"><span class="meta">#</span><span class="bash"> 查看状态</span></span><br><span class="line">rabbitmqctl status</span><br></pre></td></tr></table></figure>





<h3 id="Docker"><a href="#Docker" class="headerlink" title="Docker"></a>Docker</h3><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#</span><span class="bash"> 拉取镜像</span></span><br><span class="line">docker pull rabbitmq:3.8 # 3.8版本</span><br><span class="line">docker pull rabbitmq:3.8-management</span><br><span class="line"><span class="meta"></span></span><br><span class="line"><span class="meta">#</span><span class="bash"> 创建实例容器,实例容器在创建之后会自动运行</span></span><br><span class="line">docker run -it --rm --name rabbitmq -p 5672:5672 -p 15672:15672 rabbitmq:3.8-management</span><br><span class="line"><span class="meta"></span></span><br><span class="line"><span class="meta">#</span><span class="bash"> 进入容器</span></span><br><span class="line">docker exec -it rabbitmq /bin/bash</span><br><span class="line"><span class="meta"></span></span><br><span class="line"><span class="meta">#</span><span class="bash"> 查看rabbitmq状态</span></span><br><span class="line">rabbitmqctl status</span><br><span class="line"><span class="meta"></span></span><br><span class="line"><span class="meta">#</span><span class="bash"> 启动，</span></span><br><span class="line">rabbitmq-server -detached</span><br><span class="line"><span class="meta"></span></span><br><span class="line"><span class="meta">#</span><span class="bash"> 停止</span></span><br><span class="line">rabbitmqctl stop</span><br><span class="line"><span class="meta"></span></span><br><span class="line"><span class="meta">#</span><span class="bash"> 开启web插件</span></span><br><span class="line">rabbitmq-plugins enable rabbitmq-management</span><br></pre></td></tr></table></figure>

<p><img src="https://masuo-github-image.oss-cn-beijing.aliyuncs.com/image/20220321095802.png" alt="image-20220321095801485"></p>
<p>访问15672端口</p>
<p><img src="https://masuo-github-image.oss-cn-beijing.aliyuncs.com/image/20220321100309.png" alt="image-20220321100309435"></p>
<h2 id="使用"><a href="#使用" class="headerlink" title="使用"></a>使用</h2><p><a target="_blank" rel="noopener" href="https://www.itcast.cn/news/20200709/10353348697.shtml">https://www.itcast.cn/news/20200709/10353348697.shtml</a></p>
<p><img src="https://masuo-github-image.oss-cn-beijing.aliyuncs.com/image/20220322113118.png" alt="topicmq"></p>
<h1 id="Rocket-MQ"><a href="#Rocket-MQ" class="headerlink" title="Rocket MQ"></a>Rocket MQ</h1><h1 id="Kafka"><a href="#Kafka" class="headerlink" title="Kafka"></a>Kafka</h1>
      
    </div><!-- .entry-content -->

    <footer class="entry-meta">
    <a href="/2022/01/18/MQ/">
    <time datetime="2022-01-18T09:27:50.237Z" class="entry-date">
        2022-01-18
    </time>
</a>
    
    
    </footer>
</article>


    
<nav class="nav-single">
    <h3 class="assistive-text">文章导航</h3>
    
        <span class="nav-previous"><a href="/2022/04/08/%E5%BC%80%E5%8F%91%E8%A7%84%E8%8C%83/" rel="prev"><span class="meta-nav">←</span> 开发规范</a></span>
    
    
        <span class="nav-next"><a href="/2022/01/11/Redis/" rel="next">Redis <span class="meta-nav">→</span></a></span>
    
</nav><!-- .nav-single -->






<section id="comments">
  <!-- 多说评论框 start -->
  <div class="ds-thread" data-thread-key="post-MQ" data-title="消息中间件" data-url="https://masuo777.github.io/2022/01/18/MQ/"></div>
  <!-- 多说评论框 end -->
  <!-- 多说公共JS代码 start (一个网页只需插入一次) -->
  <script type="text/javascript">
  var duoshuoQuery = {short_name:'ms'};
    (function() {
	  var ds = document.createElement('script');
	  ds.type = 'text/javascript';ds.async = true;
	  ds.src = (document.location.protocol == 'https:' ? 'https:' : 'http:') + '//static.duoshuo.com/embed.js';
	  ds.charset = 'UTF-8';
	  (document.getElementsByTagName('head')[0]
	   || document.getElementsByTagName('body')[0]).appendChild(ds);
    })();
  </script>
  <!-- 多说公共JS代码 end -->
</section>


</div></div>
        <div id="secondary" class="widget-area" role="complementary">
  
    <aside class="widget">
    <h3 class="widget-title">CONTACT ME</h3>
    <div class="widget-content">
      <p>邮箱：masuo0.0@foxmail.com</p>
      <p>QQ：2919029885</p>
      <a target="_blank" rel="noopener" href="https://www.cnblogs.com/msdog/">博客园</a>
      </br>
      <img src="https://masuo-github-image.oss-cn-beijing.aliyuncs.com/image/20210701223301.jpg" height="250px"/>
      
    </div>
</aside>
  
    <aside class="widget">
    <h3 class="widget-title">赞助我</h3>
    <div class="widget-content">
      <img src='/css/images/alipay.png' style='width:100%;' />
    </div>
</aside>
  
    <aside class="widget">
    <h3 class="widget-title">公众号</h3>
    <div class="widget-content">
      <p>暂时没有，待我代码行数100000，我可能才有这个资格吧。</p>
      <p></p>
    </div>
</aside>
  
</div>
      </div>
      <footer id="colophon" role="contentinfo">
    <p>&copy; 2022 John Doe; All rights reserved.</p>
    <p>Powered by <a href="http://hexo.io/" target="_blank">Hexo</a></p>
</footer>
    <script>window._bd_share_config={"common":{"bdSnsKey":{},"bdText":"","bdMini":"1","bdMiniList":false,"bdPic":"","bdStyle":"2","bdSize":"16"},"share":{}};with(document)0[(getElementsByTagName('head')[0]||body).appendChild(createElement('script')).src='http://bdimg.share.baidu.com/static/api/js/share.js?v=89860593.js?cdnversion='+~(-new Date()/36e5)];</script>

<script src="/js/jquery-2.0.3.min.js"></script>


  
<link rel="stylesheet" href="/fancybox/jquery.fancybox.css">

  
<script src="/fancybox/jquery.fancybox.pack.js"></script>




<script src="/js/script.js"></script>


<script src="/js/navigation.js"></script>

<div id="bg"></div>

  </div>
</body>
</html>